/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * @author Guilherme
 */
public abstract class FilePersistence implements Serializable
{
    protected String      fileName;

    public FilePersistence(String fileName)
    {
        this.fileName = fileName;
    }      
    
    protected static boolean save(FilePersistence fpersist)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(fpersist.fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(fpersist);
            oos.close();
            
        } catch (FileNotFoundException ex)
        {
            System.err.println("[PERSIST] File not found");
        } catch (IOException ex)
        {
            System.err.println("[PERSIST] I/O Error");
        }   
        return true;
    }
    
    public static FilePersistence load(String fileName)
    {
        FilePersistence fPersist = null;
        try
        {
            FileInputStream fin = new FileInputStream(fileName);
            ObjectInputStream ois = new ObjectInputStream(fin); 
            fPersist = (FilePersistence) ois.readObject(); 
            ois.close(); 
            
        }catch(Exception ex)
        { 
            System.err.println("[PERSIST] Error");
        } 
        return fPersist;
    }
}
